package com.qtenv.datasync.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.text.csv.CsvWriter;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName Test1
 * @Author CBL
 * @Date 2022/7/13 11:08
 **/
public class Test2 {
    public static void main(String[] args) {
        List<String[]> resultList = CollectionUtil.newArrayList();

        JSONObject jsonObject = JSONUtil.parseObj(new FileReader("440118.geojson").readString());
        JSONArray jsonArray = jsonObject.getJSONArray("features");

        jsonArray.forEach(line -> {
            String coorsStr = JSONUtil.parseObj(line).getJSONObject("geometry").getStr("coordinates");
            coorsStr = coorsStr.substring(2);
            coorsStr = coorsStr.substring(0, coorsStr.length() - 2);
            JSONArray coors = JSONUtil.parseArray(coorsStr);
            coors.remove(0);
            coors.remove(0);
            coors.remove(coors.size() - 1);
            coors.remove(coors.size() - 1);
            String name = JSONUtil.parseObj(line).getJSONObject("properties").getStr("name");

            List<String> linePoints = coors.stream().map(item -> {
                String handled = item.toString().replace("[", "{lng:").replace(",", ", lat:").replace("]", "}");
                return handled;
            }).collect(Collectors.toList());
            String coordinates = String.join(",", linePoints);
            System.out.println(name + "  " + "[" + coordinates + "]");

            String[] r = new String[2];
            r[0] = name;
            r[1] = "[" + coordinates + "]";

            resultList.add(r);
        });

        CsvWriter writer = CsvUtil.getWriter("C:/Users/1/Desktop/testWrite.csv", CharsetUtil.CHARSET_UTF_8);
        //按行写出
        writer.write(resultList);
    }
}
